package org.jing.r18.game.venusblood.util;

import org.jing.core.lang.ExceptionHandler;
import org.jing.core.lang.JingException;
import org.jing.core.lang.JingInitializer;
import org.jing.core.lang.data.Carrier;
import org.jing.r18.game.venusblood.lang.ProjectConfig;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * Description: <br>
 *
 * @author: bks <br>
 * @createDate: 2025-05-13 <br>
 */
public class Helper {

    public static ProjectConfig getProjectConfig() {
        Carrier projectC = JingInitializer.getRootConfig().getCarrier("project");
        ExceptionHandler.publishIfMatch(null == projectC, "project配置节点不存在");
        return projectC.serialize(ProjectConfig.class);
    }

    public static byte[] inflate(byte[] source) {
        return inflate(source, true);
    }

    public static byte[] inflate(byte[] source, boolean nowrap) {
        Inflater inflater = new Inflater(nowrap);
        inflater.setInput(source);
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream(source.length)) {
            byte[] buff = new byte[1024];
            while (!inflater.finished()) {
                int count = inflater.inflate(buff);
                baos.write(buff, 0, count);
            }
            inflater.end();
            return baos.toByteArray();
        } catch (Exception e) {
            throw new JingException(e);
        }
    }

    public static byte[] deflate(byte[] source) {
        return deflate(source, Deflater.DEFLATED, true);
    }

    public static byte[] deflate(byte[] source, int level, boolean nowrap) {
        Deflater deflater = new Deflater(level, nowrap);
        deflater.setInput(source);
        deflater.finish();
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream(source.length)) {
            byte[] buff = new byte[source.length];
            while (!deflater.finished()) {
                int count = deflater.deflate(buff);
                baos.write(buff, 0, count);
            }
            deflater.end();
            return baos.toByteArray();
        } catch (Exception e) {
            throw new JingException(e);
        }
    }

    public static short byteArrayToShort(byte[] byteArray, int offset) {
        short res = 0;
        for (int i$ = 0; i$ < 2; ++ i$) {
            res |= (short) ((byteArray[offset + i$] & 0xff) << (i$ * 8));
        }
        return res;
    }

    public static int byteArrayToInt(byte[] byteArray, int offset) {
        int res = 0;
        for (int i$ = 0; i$ < 4; ++ i$) {
            res |= ((byteArray[offset + i$] & 0xff) << (i$ * 8));
        }
        return res;
    }

    public static long byteArrayToLong(byte[] byteArray, int offset) {
        long res = 0;
        for (int i$ = 0; i$ < 8; ++ i$) {
            res |= ((long) (byteArray[offset + i$] & 0xff) << (i$ * 8));
        }
        return res;
    }

    public static void longToByteArray(long number, byte[] byteArray, int offset) {
        for (int i$ = 0; i$ < 8; ++i$) {
            byteArray[offset + i$] = (byte) ((number >> (i$ * 8)) & 0xff);
        }
    }

    public static void intToByteArray(int number, byte[] byteArray, int offset) {
        for (int i$ = 0; i$ < 4; ++i$) {
            byteArray[offset + i$] = (byte) ((number >> (i$ * 8)) & 0xff);
        }
    }

    public static void shortToByteArray(short number, byte[] byteArray, int offset) {
        for (int i$ = 0; i$ < 2; ++i$) {
            byteArray[offset + i$] = (byte) ((number >> (i$ * 8)) & 0xff);
        }
    }
}
